package com.ulwx.tool;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;

import com.ulwx.database.DataBase;
import com.ulwx.database.DataBaseFactory;
import com.ulwx.database.DataBaseSet;
import com.ulwx.database.RowMapper;
import com.ulwx.database.DataBase.MainSlaveModeConnectMode;
import com.ulwx.database.sql.Result;
import com.ulwx.database.sql.ResultSupport;

public class DbUtils {

	public static DataBaseSet doCachedPageQuery(String pollName,
			String sqlQuery, Map<Integer, Object> vParameters, int page,
			int perPage, PageBean pageUtils, String countSql) throws Exception {
	
			return DbUtils.doCachedPageQuery(pollName,sqlQuery, vParameters, page, perPage,
					pageUtils, countSql,MainSlaveModeConnectMode.Connect_SlaveServer);
		

	}
	public static DataBaseSet doCachedPageQuery(String pollName,
			String sqlQuery, Map<Integer, Object> vParameters, int page,
			int perPage, PageBean pageUtils, String countSql,
			MainSlaveModeConnectMode mainSlaveModeConnectMode) throws Exception {
		DataBase db = null;
		try {
			if(StringUtils.isEmpty(pollName)){
				db =DataBaseFactory.getDataBase(mainSlaveModeConnectMode);
			}else{
				db = DataBaseFactory.getDataBase(mainSlaveModeConnectMode);
			}
			return db.doCachedPageQuery(sqlQuery, vParameters, page, perPage,
					pageUtils, countSql);
		} finally {
			if (db != null) {
				db.close();
			}
		}

	}
	public static DataBaseSet doCachedQuery(String pollName, String sqlQuery,
			Map<Integer, Object> vParameters) throws Exception {

			return DbUtils.doCachedQuery(pollName,sqlQuery, vParameters,
					MainSlaveModeConnectMode.Connect_SlaveServer);
	

	}
	public static DataBaseSet doCachedQuery(String pollName, String sqlQuery,
			Map<Integer, Object> vParameters,
			MainSlaveModeConnectMode mainSlaveModeConnectMode) throws Exception {
		DataBase db = null;
		try {
			if(StringUtils.isEmpty(pollName)){
				db =DataBaseFactory.getDataBase(mainSlaveModeConnectMode);
			}else{
				db = DataBaseFactory.getDataBase(pollName,
						mainSlaveModeConnectMode);
			}
			return db.doCachedQuery(sqlQuery, vParameters);
		} finally {
			if (db != null) {
				db.close();
			}
		}

	}
	public static <T> List<T> doPageQueryObject(String pollName,
			String sqlQuery, Map<Integer, Object> args, int page, int perPage,
			PageBean pageUtils, RowMapper<T> rowMapper, String countSql)
			throws Exception {
	

			return DbUtils.doPageQueryObject(pollName,
					sqlQuery, args, page, perPage,
					pageUtils, rowMapper, countSql,MainSlaveModeConnectMode.Connect_SlaveServer);
	
	}

	
	public static <T> List<T> doPageQueryObject(String pollName,
			String sqlQuery, Map<Integer, Object> args, int page, int perPage,
			PageBean pageUtils, RowMapper<T> rowMapper, String countSql,
			MainSlaveModeConnectMode mainSlaveModeConnectMode)
			throws Exception {
		DataBase db = null;
		try {
			if(StringUtils.isEmpty(pollName)){
				db =DataBaseFactory.getDataBase(mainSlaveModeConnectMode);
			}else{
				db = DataBaseFactory.getDataBase(pollName,mainSlaveModeConnectMode);
			}
			return db.doPageQueryObject(sqlQuery, args, page, perPage,
					pageUtils, rowMapper, countSql);
		} finally {
			if (db != null) {
				db.close();
			}
		}
	}
	public static <T> List<T> doQueryObject(String pollName, String sqlQuery,
			Map<Integer, Object> args, RowMapper<T> rowMapper) throws Exception {

			return DbUtils.doQueryObject(pollName,sqlQuery, args, rowMapper,
					MainSlaveModeConnectMode.Connect_SlaveServer);
	
	}
	public static <T> List<T> doQueryObject(String pollName, String sqlQuery,
			Map<Integer, Object> args, RowMapper<T> rowMapper,
			MainSlaveModeConnectMode mainSlaveModeConnectMode) throws Exception {
		DataBase db = null;
		try {
			if(StringUtils.isEmpty(pollName)){
				db =DataBaseFactory.getDataBase(mainSlaveModeConnectMode);
			}else{
				db = DataBaseFactory.getDataBase(pollName,mainSlaveModeConnectMode);
			}
			return db.doQueryObject(sqlQuery, args, rowMapper);
		} finally {
			if (db != null) {
				db.close();
			}
		}
	}
	public static <T> List<T> doQueryClass(String pollName, Class<T> clazz,
			String sqlQuery, Map<Integer, Object> vParameters) throws Exception {

			return DbUtils.doQueryClass(pollName,
					clazz, sqlQuery, vParameters,MainSlaveModeConnectMode.Connect_SlaveServer);
	
	}
	
	public static <T> List<T> doQueryClass(String pollName, Class<T> clazz,
			String sqlQuery, Map<Integer, Object> vParameters,
			MainSlaveModeConnectMode mainSlaveModeConnectMode) throws Exception {
		DataBase db = null;
		try {
			if(StringUtils.isEmpty(pollName)){
				db =DataBaseFactory.getDataBase(mainSlaveModeConnectMode);
			}else{
				db = DataBaseFactory.getDataBase(pollName,mainSlaveModeConnectMode);
			}
			return db.doQueryClass(clazz, sqlQuery, vParameters);
		} finally {
			if (db != null) {
				db.close();
			}
		}
	}

	
	public static <T> List<T> doPageQueryClass(String pollName, Class<T> clazz,
			String sqlQuery, Map<Integer, Object> vParameters, int page,
			int perPage, PageBean pageUtils, String countSql,
			MainSlaveModeConnectMode mainSlaveModeConnectMode) throws Exception {
		DataBase db = null;
		try {
			if(StringUtils.isEmpty(pollName)){
				db =DataBaseFactory.getDataBase(mainSlaveModeConnectMode);
			}else{
				db = DataBaseFactory.getDataBase(pollName,mainSlaveModeConnectMode);
			}
			return db.doPageQueryClass(clazz, sqlQuery, vParameters, page,
					perPage, pageUtils, countSql);
		} finally {
			if (db != null) {
				db.close();
			}
		}
	}
	public static <T> List<T> doPageQueryClass(String pollName, Class<T> clazz,
			String sqlQuery, Map<Integer, Object> vParameters, int page,
			int perPage, PageBean pageUtils, String countSql) throws Exception {

			return DbUtils.doPageQueryClass(pollName,clazz, sqlQuery, vParameters, page,
					perPage, pageUtils, countSql,MainSlaveModeConnectMode.Connect_SlaveServer);
	
	}

	
	public static <T> List<T> doQueryClassNoSql(String pollName,
			Class<T> clazz, T selectObject, String selectProperties,
			Map<Integer, Object> vParameters,MainSlaveModeConnectMode mainSlaveModeConnectMode) throws Exception {
		DataBase db = null;
		try {
			if(StringUtils.isEmpty(pollName)){
				db =DataBaseFactory.getDataBase(mainSlaveModeConnectMode);
			}else{
				db = DataBaseFactory.getDataBase(pollName,mainSlaveModeConnectMode);
			}
			return db.doQueryClassNoSql(clazz, selectObject, selectProperties,
					vParameters);
		} finally {
			if (db != null) {
				db.close();
			}
		}
	}
	public static <T> List<T> doQueryClassNoSql(String pollName,
			Class<T> clazz, T selectObject, String selectProperties,
			Map<Integer, Object> vParameters) throws Exception {
	
			return DbUtils.doQueryClassNoSql(pollName,clazz, selectObject, selectProperties,
					vParameters,MainSlaveModeConnectMode.Connect_SlaveServer);
		
	}
	public static <T> List<T> doQueryClassNoSql(String pollName,
			Class<T> clazz, T selectObject, String selectProperties,
			MainSlaveModeConnectMode mainSlaveModeConnectMode) 
			throws Exception {
		DataBase db = null;
		try {
			if(StringUtils.isEmpty(pollName)){
				db =DataBaseFactory.getDataBase(mainSlaveModeConnectMode);
			}else{
				db = DataBaseFactory.getDataBase(pollName,mainSlaveModeConnectMode);
			}
			return db.doQueryClassNoSql(clazz, selectObject, selectProperties
					);
		} finally {
			if (db != null) {
				db.close();
			}
		}
	}
	public static <T> List<T> doQueryClassNoSql(String pollName,
			Class<T> clazz, T selectObject, String selectProperties) 
			throws Exception {
	
	
			return DbUtils.doQueryClassNoSql(pollName,
					clazz, selectObject, selectProperties,
					MainSlaveModeConnectMode.Connect_SlaveServer);
		
	}

	public static <T> int excuteDeleteClass(String pollName, T deleteObject,
			String deleteProperteis) throws Exception {
		DataBase db = null;
		try {
			if(StringUtils.isEmpty(pollName)){
				db =DataBaseFactory.getDataBase();
			}else{
				db = DataBaseFactory.getDataBase(pollName);
			}
			return db.excuteDeleteClass(deleteObject, deleteProperteis);
		} finally {
			if (db != null) {
				db.close();
			}
		}
	}

	public static  <T> int[] excuteDeleteClass(String pollName, T[] deleteObject,
			String deleteProperteis) throws Exception {
		DataBase db = null;
		try {
			if(StringUtils.isEmpty(pollName)){
				db =DataBaseFactory.getDataBase();
			}else{
				db = DataBaseFactory.getDataBase(pollName);
			}
			return db.excuteDeleteClass(deleteObject, deleteProperteis);
		} finally {
			if (db != null) {
				db.close();
			}
		}
	}

	public static  int[] excuteDeleteObjects(String pollName, Object[] deleteObjects,
			String[] deletePropertiesArray) throws Exception {
		DataBase db = null;
		try {
			if(StringUtils.isEmpty(pollName)){
				db =DataBaseFactory.getDataBase();
			}else{
				db = DataBaseFactory.getDataBase(pollName);
			}
			return db.excuteDeleteObjects(deleteObjects, deletePropertiesArray);
		} finally {
			if (db != null) {
				db.close();
			}
		}
	}

	public static <T> int excuteInsertClass(String pollName, T insertObject)
			throws Exception {
		DataBase db = null;
		try {
			if(StringUtils.isEmpty(pollName)){
				db =DataBaseFactory.getDataBase();
			}else{
				db = DataBaseFactory.getDataBase(pollName);
			}
			return db.excuteInsertClass(insertObject);
		} finally {
			if (db != null) {
				db.close();
			}
		}

	}

	public static  <T> int excuteInsertClass(String pollName, T insertObject,
			String[] properties) throws Exception {
		DataBase db = null;
		try {
			if(StringUtils.isEmpty(pollName)){
				db =DataBaseFactory.getDataBase();
			}else{
				db = DataBaseFactory.getDataBase(pollName);
			}
			return db.excuteInsertClass(insertObject, properties);
		} finally {
			if (db != null) {
				db.close();
			}
		}
	}

	public static  <T> int[] excuteInsertClass(String pollName, T[] objs)
			throws Exception {
		DataBase db = null;
		try {
			if(StringUtils.isEmpty(pollName)){
				db =DataBaseFactory.getDataBase();
			}else{
				db = DataBaseFactory.getDataBase(pollName);
			}
			return db.excuteInsertClass(objs);
		} finally {
			if (db != null) {
				db.close();
			}
		}
	}

	public static <T> int[] excuteInsertClass(String pollName, T[] insertObjects,
			String[] properties) throws Exception {
		DataBase db = null;
		try {
			if(StringUtils.isEmpty(pollName)){
				db =DataBaseFactory.getDataBase();
			}else{
				db = DataBaseFactory.getDataBase(pollName);
			}
			return db.excuteInsertClass(insertObjects, properties);
		} finally {
			if (db != null) {
				db.close();
			}
		}
	}
	public static  <T> int excuteUpdateClass(String pollName,T updateObject, String beanKey)
	throws Exception {
		DataBase db = null;
		try {
			if(StringUtils.isEmpty(pollName)){
				db =DataBaseFactory.getDataBase();
			}else{
				db = DataBaseFactory.getDataBase(pollName);
			}
			return db.excuteUpdateClass(updateObject, beanKey);
		} finally {
			if (db != null) {
				db.close();
			}
		}
	}
	public static <T> int excuteUpdateClass(String pollName,
			T updateObject, String beanKey,
			String[] properties) throws Exception {
		DataBase db = null;
		try {
			if(StringUtils.isEmpty(pollName)){
				db =DataBaseFactory.getDataBase();
			}else{
				db = DataBaseFactory.getDataBase(pollName);
			}
			return db.excuteUpdateClass(updateObject, beanKey, properties);
		} finally {
			if (db != null) {
				db.close();
			}
		}
	}
	
	public static <T> int[] excuteUpdateClass(String pollName,T[] objects, String beanKey,
			String[] properties) throws Exception {
		DataBase db = null;
		try {
			if(StringUtils.isEmpty(pollName)){
				db =DataBaseFactory.getDataBase();
			}else{
				db = DataBaseFactory.getDataBase(pollName);
			}
			return db.excuteUpdateClass(objects, beanKey, properties);
		} finally {
			if (db != null) {
				db.close();
			}
		}
	}
	
	public static int[] excuteUpdateObjects(String pollName,Object[] objects, String[] beanKeys,
			String[][] properties) throws Exception {
		DataBase db = null;
		try {
			if(StringUtils.isEmpty(pollName)){
				db =DataBaseFactory.getDataBase();
			}else{
				db = DataBaseFactory.getDataBase(pollName);
			}
			return db.excuteUpdateObjects(objects, beanKeys, properties);
		} finally {
			if (db != null) {
				db.close();
			}
		}
	}
	
	public static int[] executeBatch(String pollName,ArrayList<String> sqltxt) throws Exception {
		DataBase db = null;
		try {
			if(StringUtils.isEmpty(pollName)){
				db =DataBaseFactory.getDataBase();
			}else{
				db = DataBaseFactory.getDataBase(pollName);
			}
			return db.executeBatch(sqltxt);
		} finally {
			if (db != null) {
				db.close();
			}
		}
	}
	
	public  static int[] executeBindBatch(String pollName,String sqltxt,
			List<Map<Integer, Object>> vParametersList) throws Exception {
		DataBase db = null;
		try {
			if(StringUtils.isEmpty(pollName)){
				db =DataBaseFactory.getDataBase();
			}else{
				db = DataBaseFactory.getDataBase(pollName);
			}
			return db.executeBindBatch(sqltxt, vParametersList);
		} finally {
			if (db != null) {
				db.close();
			}
		}
	}
	
	public  static int[] executeBindBatch(String pollName,String[] sqltxts,
			Map<Integer, Object>[] vParametersArray) throws Exception {
		DataBase db = null;
		try {
			if(StringUtils.isEmpty(pollName)){
				db =DataBaseFactory.getDataBase();
			}else{
				db = DataBaseFactory.getDataBase(pollName);
			}
			return db.executeBindBatch(sqltxts, vParametersArray);
		} finally {
			if (db != null) {
				db.close();
			}
		}
	}

	public static  int executeBindDelete(String pollName,String sqltext,
			Map<Integer, Object> vParameters) throws Exception {
		DataBase db = null;
		try {
			if(StringUtils.isEmpty(pollName)){
				db =DataBaseFactory.getDataBase();
			}else{
				db = DataBaseFactory.getDataBase(pollName);
			}
			return db.executeBindDelete(sqltext, vParameters);
		} finally {
			if (db != null) {
				db.close();
			}
		}
	}
	
	public static int executeBindInsert(String pollName,String sqltext,
			Map<Integer, Object> vParameters) throws Exception {
		DataBase db = null;
		try {
			if(StringUtils.isEmpty(pollName)){
				db =DataBaseFactory.getDataBase();
			}else{
				db = DataBaseFactory.getDataBase(pollName);
			}
			return db.executeBindInsert(sqltext, vParameters);
		} finally {
			if (db != null) {
				db.close();
			}
		}
	}
	
	public static int executeBindInsertReturnKey(String pollName,String sqltext,
			Map<Integer, Object> vParameters) throws Exception {
		DataBase db = null;
		try {
			if(StringUtils.isEmpty(pollName)){
				db =DataBaseFactory.getDataBase();
			}else{
				db = DataBaseFactory.getDataBase(pollName);
			}
			return db.executeBindInsertReturnKey(sqltext, vParameters);
		} finally {
			if (db != null) {
				db.close();
			}
		}
	}
	
	public static int executeBindUpdate(String pollName,String sqltext,
			Map<Integer, Object> vParameters) throws Exception {
		DataBase db = null;
		try {
			if(StringUtils.isEmpty(pollName)){
				db =DataBaseFactory.getDataBase();
			}else{
				db = DataBaseFactory.getDataBase(pollName);
			}
			return db.executeBindUpdate(sqltext, vParameters);
		} finally {
			if (db != null) {
				db.close();
			}
		}
	}
	
	
	public static int executeStoredProcedure(String pollName,String sqltext,
			Map<String, Object> parms, Map<Integer, Object> outPramsValues,
			List<DataBaseSet> returnDataBaseSets) throws Exception {
		DataBase db = null;
		try {
			if(StringUtils.isEmpty(pollName)){
				db =DataBaseFactory.getDataBase();
			}else{
				db = DataBaseFactory.getDataBase(pollName);
			}
			return db.executeStoredProcedure(sqltext, parms, outPramsValues, returnDataBaseSets);
		} finally {
			if (db != null) {
				db.close();
			}
		}
	}
	
	public static Map<String,Object>[] toMapArray(DataBaseSet dbs){
		
		Result result=ResultSupport.toResult(dbs.getRowSet());
		return result.getRows();
		
		
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
